package com.bit.demo.service;

import com.bit.demo.enums.BookStatus;
import com.bit.demo.mapper.BookInfoMapper;
import com.bit.demo.model.BookInfo;
import com.bit.demo.model.PageRequest;
import com.bit.demo.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {

    @Autowired
    private BookInfoMapper bookInfoMapper;

    public void addBook(BookInfo bookInfo) {
        bookInfoMapper.addBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
        int count = bookInfoMapper.count();
        List<BookInfo> books = bookInfoMapper.getListByPage(pageRequest);
        for(BookInfo book : books) {
            book.setStatusCN(BookStatus.getDesByCode(book.getStatus()).getDes());
        }
        return new PageResult<>(count, books, pageRequest);
    }

    public BookInfo queryBookById(Integer id) {
        return bookInfoMapper.queryBookById(id);
    }

    public void updateBook(BookInfo bookInfo) {
        bookInfoMapper.updateBook(bookInfo);
    }

    public Integer deleteBook(Integer id) {
        return bookInfoMapper.deleteBook(id);
    }

    public void batchDeleteBook(List<Integer> ids) {
        bookInfoMapper.batchDeleteBook(ids);
    }
}
